package com.shiva.oms.logview.api.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.lang3.StringUtils;

import com.sf.shiva.oms.common.constant.LogTableConstant;
import com.sf.shiva.oms.common.util.DateUtil;
import com.shiva.oms.logview.domain.LogData;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年4月19日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
public class LogDataTableModel extends AbstractTableModel {

    private List<LogData> datas;
    private String[] headNames;
    private Properties parseProperties;

    public LogDataTableModel() {
        super();
    }
    public LogDataTableModel(List<LogData> list) {
        this();
        this.datas = list;
        this.headNames = new String[TableColumns.values().length];
        fireTableDataChanged();
    }

    public LogDataTableModel(List<LogData> list, String[] headNames) {
        this();
        this.datas = list;
        this.headNames = headNames;
    }
    
    public LogDataTableModel(List<LogData> list, String[] headNames, Properties parseProperties) {
        this(list, headNames);
        this.parseProperties = parseProperties;
    }

    public LogDataTableModel(List<LogData> list, TableColumns[] headNames) {
        this();
        this.datas = list;
        this.headNames = createHeadColumns(headNames);
    }

    private String[] createHeadColumns(TableColumns[] headNames) {
        if (headNames != null) {
            String[] names = new String[headNames.length];
            for (int i = 0; i < headNames.length; i++) {
                names[i] = headNames[i].getHeadName();
            }
            return names;
        }
        return null;
    }

    public String[] getHeadNames() {
        return headNames;
    }

    public void setHeadNames(String[] headNames) {
        this.headNames = headNames;
    }

    public void setHeadNames(TableColumns[] headNames) {
        this.headNames = createHeadColumns(headNames);
    }

    @Override
    public int getColumnCount() {
        return this.headNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return this.headNames[column];
    }

    @Override
    public int getRowCount() {
        return this.datas.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        LogData data = this.datas.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return data.getId();
            case 1:
                String dateStr = "";
                if (this.parseProperties!=null && null!= this.parseProperties.get(LogTableConstant.PARSING_PROPERTY_DATE_FORMAT)){
                    String dateFormat= (String) this.parseProperties.get(LogTableConstant.PARSING_PROPERTY_DATE_FORMAT);
                    dateStr = DateUtil.formatDate(data.getDate(), dateFormat);
                }
                if (StringUtils.isEmpty(dateStr)) {
                    dateStr = DateUtil.date2String(data.getDate());
                }
                return dateStr;
            case 2:
                return data.getLevel().toString();
            case 3:
                return data.getClazz();
            case 4:
                return data.getMethod();
            case 5:
                return data.getMessage();
            default:
                return "-";
        }
    }

    public LogData getRow(int rowIndex) {
        return this.datas.get(rowIndex);
    }

    public void clear() {
        this.datas = new ArrayList<>();
        fireTableDataChanged();
    }

    public List<LogData> getDatas() {
        return datas;
    }

    public void setDatas(List<LogData> datas) {
        this.datas = datas;
    }

    public void setDatas(List<LogData> datas, TableColumns[] headNames, Properties parseProperties) {
        this.datas = datas;
        this.headNames = createHeadColumns(headNames);
        this.parseProperties= parseProperties;
    }

    public void setDatas(List<LogData> datas, String[] headNames, Properties parseProperties) {
        this.datas = datas;
        this.headNames = headNames;
        this.parseProperties = parseProperties;
    }

}
